import { createRouter, createWebHistory } from 'vue-router'

// import { home } from './home'
import { nav } from './nav'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login', //重定向redirect
      meta: {
        title: '登录'
      }
    },

    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/loginIndex.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/codeTest',
      name: 'codeTest',
      component: () => import('@/views/codeTest/index.vue'),
      meta: {
        title: '测试'
      }
    },
    {
      path: '/home',
      name: 'Home',
      component: () => import('@/views/home/homeIndex.vue'),
      meta: {
        title: '首页'
      },
      children: [...nav]
    }
  ]
})
// 变化名字
router.beforeEach((to, from, next) => {
  const userId = sessionStorage.getItem('userId')
  if (`${userId}` !== '1' && to.path === '/nav/userAdmin') {
    next('/')
  }
  document.title = to.meta.title
  next()
})

export default router
